﻿(function () {
    oo('class', {
        prop: [
            { name: 'data', isStatic: true },
            { name: 'selectIndex', value: 0, isStatic: true },
            {
                name: 'quantity', value: 10, isStatic: true,
                setter: function (value) {
                    $('#txtProductQuantity').val(value);
                    this(value);
                }
            },
            { name: 'addToBasketCallback', isStatic: true }
        ],
        $product: function () {
            $('#divProduct').show().dialog({
                title: '商品明细',
                width: 680,
                height: 435,
                modal: true
            }).dialog('close');
            $('#txtProductQuantity').numberbox({
                min: 1
            });
        },
        _product: null,
        init: function (data) {
            this.prop('data', data);
            $('#divProductList').setTemplateElement('txtProductListTemplate').processTemplate(data);
        },
        showDetail: function (index) {
            this.prop('selectIndex', index);
            $('#divProduct').setTemplateElement('txtProductTemplate').processTemplate(this.prop('data')[index]).dialog();
            this.prop('quantity', 10);
        },
        increaseQuantity: function () {
            var quantity = this.prop('quantity');
            if (1 < quantity)
                this.prop('quantity', --quantity);
        },
        decreaseQuantity: function () {
            this.prop('quantity', this.prop('quantity') + 1);
        },
        chooseBigUnit: function (id, num) {
            var product = this.prop('data')[this.prop('selectIndex')];
            $.each(product.bigUnits, (function (self) {
                return function () {
                    if (this.id == id) {
                        self.prop('quantity', this.count * num);
                        return false;
                    }
                };
            })(this));
        },
        addToBasket: function () {
            $('#divProduct').dialog('close');
            var product = this.prop('data')[this.prop('selectIndex')];
            var callback;
            (callback = this.prop('addToBasketCallback')) && callback(product.id, this.prop('quantity'));
        }
    });
})();